package com.ztgf.commodity.api.cms;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.CategoryMaintainNewSaveBO;
import com.ztgf.commodity.models.dto.CategoryImportDTO;
import com.ztgf.commodity.models.dto.CategoryImportErrorDateDTO;
import com.ztgf.commodity.models.service.ICategoryImportErrorDateService;
import com.ztgf.commodity.models.service.ICategoryImportService;
import com.ztgf.commodity.models.service.ICategoryMaintainRecordService;
import com.ztgf.commodity.models.service.ICategoryMaintainService;
import com.ztgf.commodity.models.vo.CategoryAttributesBean;
import com.ztgf.commodity.models.vo.CategoryImportErrorVO;
import com.ztgf.commodity.models.vo.CategoryMaintainRecordVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.ExcelExport2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cms/categoryMaintainCms")
@Api(value = "CategoryMaintainController", tags = "类目维护cms接口")
public class CategoryMaintainController {

    @DubboReference
    private ICategoryMaintainService categoryMaintainService;
    @DubboReference
    private ICategoryMaintainRecordService categoryMaintainRecordService;
    @DubboReference
    private ICategoryImportErrorDateService categoryImportErrorDateService;
    @DubboReference
    private ICategoryImportService categoryImportService;

    @PostMapping(value = "/queryCategoryBrandAttributes")
    @ApiOperation(value = "根据类目id和品牌id查询所关联的属性", notes = "根据类目id和品牌id查询所关联的属性")
    public Result queryCategoryBrandAttributes(Long categoryId, Long brandId) {
        if (categoryId == null || brandId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        CategoryAttributesBean result = categoryMaintainService.queryCategoryBrandAttributes(categoryId, brandId);
        return new Result<>(result);
    }

    @PostMapping(value = "/insertCategoryMaintenance")
    @ApiOperation(value = "保存类目维护数据", notes = "保存类目维护数据")
    public Result insertCategoryMaintenance(String object) {
        log.info("保存类目维护数据 : {}", object);
        List<CategoryMaintainNewSaveBO> boList = JSONObject.parseArray(object, CategoryMaintainNewSaveBO.class);
        if (CustomizeUtil.listIsNull(boList)) {
            return new Result<>(CodeEnum.FAIL_COMMODITY_ERROR_340000);
        } else {
            log.info("类目维护数据格式化后:{}", boList);
            categoryMaintainService.insertCategoryMaintenance(boList);
            return new Result<>(CodeEnum.SUCCESS);
        }
    }

    @PostMapping(value = "/queryPageList")
    @ApiOperation(value = "查询类目维护表分页列表", notes = "查询类目维护表分页列表")
    public Result queryPageList(Integer pageNo, Integer pageSize) {
        Page<CategoryMaintainRecordVO> page = categoryMaintainRecordService.queryPageList(pageNo, pageSize);
        return new Result<>(page);
    }

    @SneakyThrows
    @PostMapping(value = "/importCategoryAttributes")
    @ApiOperation(value = "后台类目管理导入属性返回导入的信息", notes = "后台类目管理导入属性返回导入的信息")
    public Result importAttributes(@RequestParam("file") MultipartFile file) {
        if (null == file || file.isEmpty()) {
            return new Result<>(CodeEnum.FAIL_FILE_400000);
        }
        byte[] input = file.getBytes();
        String fileName = file.getOriginalFilename();
        boolean flag = categoryMaintainService.importCategoryAttributes(input, fileName);
        if (flag) {
            categoryMaintainService.addImportDate();
        }
        return new Result<>(flag);
    }

    @GetMapping(value = "/exportImportErrorDate/{id}")
    @ApiOperation(value = "导出错误的导入数据", notes = "导出错误的导入数据")
    public void exportImportErrorDate(HttpServletResponse response, @PathVariable Long id) {
        String[] attributeNames = new String[]{"序号", "0级类目", "1级类目", "2级类目", "三级类目", "品牌", "单位", "销售属性", "参数属性", "标签属性"};
        List<CategoryImportErrorVO> listA = new ArrayList<>();
        Wrapper<CategoryImportErrorDateDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("import_id", id);
        List<CategoryImportErrorDateDTO> list = categoryImportErrorDateService.selectList(wrapper);
        if (list != null && list.size() > 0) {
            for (CategoryImportErrorDateDTO dto : list) {
                CategoryImportErrorVO vo = new CategoryImportErrorVO();
                vo.setOrderNumber(dto.getOrderNumber());
                vo.setZeroCategory(dto.getZeroCategory());
                vo.setOneCategory(dto.getOneCategory());
                vo.setTwoCategory(dto.getTwoCategory());
                vo.setSanCategory(dto.getSanCategory());
                vo.setBrandName(dto.getBrandName());
                vo.setUnitName(dto.getUnitName());
                vo.setSaleAttribute(dto.getSaleAttribute());
                vo.setParamAttribute(dto.getParamAttribute());
                vo.setLabelAttribute(dto.getLabelAttribute());
                listA.add(vo);
            }
        } else {
            CategoryImportErrorVO vo = new CategoryImportErrorVO();
            vo.setOrderNumber("暂无信息");
            vo.setZeroCategory("暂无信息");
            vo.setOneCategory("暂无信息");
            vo.setTwoCategory("暂无信息");
            vo.setSanCategory("暂无信息");
            vo.setBrandName("暂无信息");
            vo.setUnitName("暂无信息");
            vo.setSaleAttribute("暂无信息");
            vo.setParamAttribute("暂无信息");
            vo.setLabelAttribute("暂无信息");
            listA.add(vo);
        }
        ExcelExport2.export(response, listA, attributeNames, "_导入的错误信息");
    }

    @PostMapping(value = "/queryImportList")
    @ApiOperation(value = "获取导入类目的文件列表", notes = "获取导入类目的文件列表")
    private Result queryImportList(Integer pageNo, Integer pageSize) {
        Wrapper<CategoryImportDTO> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time", false);
        Page<CategoryImportDTO> page = new Page<>(pageNo, pageSize);
        Page<CategoryImportDTO> selectPage = categoryImportService.selectPage(page, wrapper);
        return new Result<>(selectPage);
    }

}
